package vn.com.pavibook.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.pavibook.dao.MoneyDao;
import vn.com.pavibook.domain.model.Money;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.model.vo.MoneyVO;
import vn.com.pavibook.utils.DaoHelper;

@Repository
public class MoneyDaoImpl extends DaoHelper<Money> implements MoneyDao{

	@SuppressWarnings("rawtypes")
	class MoneyRowMapper implements RowMapper{
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			Money money = new Money();
			money.setId(rs.getInt("id"));
			money.setName(rs.getString("name"));
			money.setCurrency(rs.getString("currency"));
			money.setRate(rs.getDouble("rate"));
			money.setActive(rs.getBoolean("active"));
			return money;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<Money> getMoney(PagingInfo pagingInfo, String keyword) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM money WHERE active = 1 ");
		if (StringUtils.isNotEmpty(keyword)) {
			sql.append("AND (");
			String[] keyStrings = keyword.split(" ");
			for (int i = 0; i < keyStrings.length; i++) {
				String s = "'%" + keyStrings[i] + "%'";
				if (i == 0) {
					sql.append(" name LIKE " + s + " OR currency LIKE " + s);
				} else {
					sql.append("OR name LIKE " + s + " OR currency LIKE " + s);
				}
			}
			sql.append(")");
		}
		//sql.append(" ORDER BY create_date DESC");
		return queryPaging(sql.toString(), null, new MoneyRowMapper(),
				pagingInfo);
	}
	
	@Override
	public void deleteMoney(int id) {
		String sql= "Update money set active = 0 Where id = ?";
		Object[] params = new Object[] {id};
		insert(sql, params);
		
	}

	@Override
	public boolean checkExistCurrency(String newCurrency) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT count(id) FROM money WHERE active = 1 AND currency = '"
				+ newCurrency + "'");
		int rowNum;
		try {
			rowNum = getJdbcTemplate().queryForInt(sql.toString());
		} catch (DataAccessException e) {
			return true;
		}
		if (rowNum > 0) {
			return true;
		}
		return false;
	}
	
	@Override
	public void saveMoney(MoneyVO moneyVO) {
		String sql= "Insert Into money (id,name,currency,rate,active) Value (null,?,?,?,?)";
		Object[] params = new Object[] {moneyVO.getName(),moneyVO.getCurrency(),moneyVO.getRate(),moneyVO.isActive()};
		insert(sql, params);
		
	}
	
	@Override
	public void editMoney(MoneyVO moneyVO) {
		String sql= "UPDATE money SET rate = ? WHERE id = ?";
		Object[] params = new Object[] {moneyVO.getRate(),moneyVO.getId()};
		insert(sql, params);
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Money getMoneyById(int id) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT * FROM money B WHERE B.id = ?");
		Object[] param = new Object[] { id };
		return query(sql.toString(), param, new MoneyRowMapper())
				.getSearchResult().get(0);
	}
}
